﻿using HR60_PanelSoft.Model;
using HR60_PanelSoft.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace HR60_PanelSoft.NetWork.Base
{
   abstract class BaseSender
    {
        
        //包
        public Package package;

       /**
        * 
        * 
        * */
        public void send(){

            if (package == null)
                return ;

            byte[] data = Package.packageToByte(package);
            LogUils.info("BaseSender", "send():" + ByteUils.byte2HexString(data, " "));
            if (Config.sendType == 0)
            {
                NetUtils.returnData = null;
                int retry=0;
                while ((NetUtils.returnData == null) && retry < 3)
                {
                    NetUtils.sendDataBySerial(data);

                    Thread.Sleep(2000);
                    retry++;
                }
                
                if( NetUtils.returnData==null)
                     throw  new Exception("设备无响应,或者网络问题请重试！！");
                
                byte[] rs = NetUtils.returnData;
                LogUils.info("BaseSender", "returnData():" + ByteUils.byte2HexString(rs, " "));

                if (!PackageAck.parsePackage(rs))
                {
                    throw  new Exception("发送报文异常");
                }

            }
            else if (Config.sendType == 1)
            {
                byte[] rs =null;
                int retry=0;

                while ((NetUtils.returnData == null) && retry < 3)
                {
                    NetUtils.sendDataByUDP(data);
                    try
                    {

                        rs = NetUtils.recvSendClientUDPData();
                        if (rs != null)
                            break;
                    }
                    catch (Exception e)
                    {
                        LogUils.error("send udp retry:" + retry, e);
                    }
                  
                    retry++;

                }

              
              
                if (rs == null)
                    throw new Exception("设备无响应,或者网络问题请重试！！");

                LogUils.info("BaseSender", "returnData():" + ByteUils.byte2HexString(rs, " "));
                if (!PackageAck.parsePackage(rs))
                {
                    throw new Exception("发送报文异常");
                }
            }
            else if (Config.sendType == 2)
            {
               

            }



        }

     public    string getSettingString()
        {
            if (package == null)
                return "";
            byte[] data = Package.packageToByte(package);

            LogUils.info("getSettingString()", ByteUils.byte2HexString(data, " "));
            return ByteUils.byte2HexString(data," ");
           
        }


  

   protected int getGolobDeviceNo(Net net, Device d)
   {
       int i = 0;
       foreach (Panel panel in net.panels)
       {
           foreach (Pin pin in panel.loadPins)
           {
               if (pin.uuid == d.getConnnectedPins()[0].uuid)
               {  
                   return i;
               }
               else if (pin.connectedDevice!=null)
               {
                   i++;
               }
           }

       }

       return -1;

   }




    }
}
